aboutsummaryrefslogtreecommitdiff
path: root/src/routes/player/[player]/[profile]/+page.ts
blob: 34a4e2a0a569bbf116b969e26ee2b554efe4c554 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import type { CleanMemberProfile } from '$lib/APITypes'
import type { PageLoad } from './$types'
import { loadPack } from '$lib/packs'
import { fetchApi } from '$lib/api'
import { error, redirect } from '@sveltejs/kit'

export const load = (async ({ params, fetch, url }) => {
	const player: string = params.player
	const profile: string = params.profile
	const data: CleanMemberProfile = await fetchApi(
		`player/${player}/${profile}?customization=true`,
		fetch
	).then(async r => {
		const text = await r.text()
		try {
			return JSON.parse(text)
		} catch (e) {
			throw new Error(`Invalid JSON: ${text}`)
		}
	})

	if (!data.member) {
		throw error(404, 'Unknown profile')
	}

	if (data.member.username !== player) {
		throw redirect(302, `/player/${data.member.username}/${data.profile.name}`)
	}
	if (!data.member.left && data.profile.name !== profile) {
		throw redirect(302, `/player/${data.member.username}/${data.profile.name}`)
	}

	const packName = url.searchParams.get('pack') ?? data?.customization?.pack
	const pack = await loadPack(packName)

	return {
		...data,
		pack,
	}
}) satisfies PageLoad